<script setup lang="ts">
import {onUnmounted, reactive, ref} from 'vue';
import {login} from "@/api/login";
import {copyRight, unitInfo} from "@/api/dic";

const OrganizationInfo = ref('');
const CopyRightInfo = ref('');

/**
 * 获取版权信息
 * @constructor
 */
const InitInfo = async () => {
  try {
    CopyRightInfo.value = await copyRight();
    OrganizationInfo.value = await unitInfo();
  } catch (error) {
    console.error("初始化信息失败：", error);
  }
};

InitInfo();

let refreshTokenTimer: number | null = null;

const handleLogin = async () => {
  await login(formData.username, formData.password);
};

// 组件卸载时清除定时器
onUnmounted(() => {
  if (refreshTokenTimer) {
    clearInterval(refreshTokenTimer);
  }
});

//表单
const formDataRef = ref();
let formData = reactive({
  username: "",
  password: ""
});
const rules = {
  username: [{
    required: true,
    message: "请输入用户名"
  }],
  password: [{
    required: true,
    message: "请输入密码"
  }],
}
</script>

<template>
  <div class="login-body">
    <div class="login-panel">
      <div class="login-head">{{ OrganizationInfo }}</div>
      <div class="login-title">用户登录</div>
      <el-form :model="formData" :rules="rules" ref="formDataRef">
        <el-form-item prop="username">
          <el-input placeholder="请输入账号" v-model="formData.username" size="large" type="text">
            <template #prefix>
              <el-icon class="icon-success">
                <i-ep-user/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="formData.password" size="large" type="password"
                    @keyup.enter.native="handleLogin()">
            <template #prefix>
              <el-icon>
                <i-ep-lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="">
            <el-checkbox label="记住密码" name="type" />
        </el-form-item> -->
        <el-form-item label="">
          <el-button type="primary" style="width: 100%;" @click="handleLogin()" size="large">登录</el-button>
        </el-form-item>
      </el-form>
      <div class="login-foot">{{ CopyRightInfo }}</div>
    </div>
  </div>
</template>

<style lang="css" scoped>
.login-body {
  background: url("../assets/images/login.png") no-repeat center center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: absolute;
  left: 0;
  top: 0;

  .login-panel {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;

    padding: 25px;
    width: 26%;
    min-width: 460px;
    height: 30%;
    min-height: 300px;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 5%;
    box-shadow: 2px 2px 10px #ddd;

    .login-head{
      font-size: 22px;
      text-align: center;
      margin-bottom: 22px;
    }

    .login-title {
      font-size: 18px;
      text-align: left;
      margin-bottom: 22px;
    }

    .login-foot {
      font-size: 14px;
      text-align: center;
      margin-bottom: 22px;
    }
  }
}
</style>